home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_197 / stevie / alloc.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  211 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
  7.  */
  8.  
  9. #include "stevie.h"
  10.  
  11. /*
  12.  * This file contains various routines dealing with allocation and
  13.  * deallocation of data structures. 
  14.  */
  15.  
  16. char           *
  17. alloc(size)
  18.     unsigned        size;
  19. {
  20.     char           *p;        /* pointer to new storage space */
  21. #ifdef AMIGA
  22.     char           *pp;
  23.  
  24.     /*
  25.      * Before allocating any memory make sure there is enough for a system
  26.      * requester left. 
  27.      */
  28.     pp = malloc((unsigned) 40000);
  29.     if (pp == (char *) NULL) {    /* if there is no more room... */
  30.     emsg("alloc() is unable to find memory!");
  31.     return (NULL);
  32.     }
  33. #endif
  34.  
  35.     p = malloc(size);
  36.     if (p == (char *) NULL)    /* if there is no more room... */
  37.     emsg("alloc() is unable to find memory!");
  38.  
  39. #ifdef AMIGA
  40.     free(pp);
  41. #endif
  42.     return (p);
  43. }
  44.  
  45. char           *
  46. strsave(string)
  47.     char           *string;
  48. {
  49.     return (strcpy(alloc((unsigned) (strlen(string) + 1)), string));
  50. }
  51.  
  52. void
  53. screenalloc()
  54. {
  55.     int             i;
  56.  
  57.     /*
  58.      * If we're changing the size of the screen, free the old arrays 
  59.      */
  60.     if (Realscreen != NULL)
  61.     free(Realscreen);
  62.     if (Nextscreen != NULL)
  63.     free(Nextscreen);
  64.     if (LinePointers != NULL)
  65.     free((char *) LinePointers);
  66.     if (LineSizes != NULL)
  67.     free(LineSizes);
  68.  
  69.     Realscreen = malloc((unsigned) (Rows * Columns));
  70.     Nextscreen = malloc((unsigned) (Rows * Columns));
  71.     LinePointers = (LINE **) malloc((unsigned) (Rows * sizeof(LINE *)));
  72.     LineSizes = malloc((unsigned) Rows);
  73.  
  74.     for (i = 0; i < Rows; i++) {
  75.     LinePointers[i] = NULL;
  76.     LineSizes[i] = 0;
  77.     }
  78. }
  79.  
  80. /*
  81.  * Allocate and initialize a new line structure with room for 'nchars'
  82.  * characters. 
  83.  */
  84. LINE           *
  85. newline(nchars)
  86.     int             nchars;
  87. {
  88.     register LINE  *l;
  89.  
  90.     l = (LINE *) alloc((unsigned) sizeof(LINE));
  91.     if (l == NULL)
  92.     return (LINE *) NULL;
  93.  
  94.     l->s = alloc((unsigned) nchars);    /* the line is empty */
  95.     l->s[0] = NUL;
  96.     l->size = nchars;
  97.  
  98.     l->prev = (LINE *) NULL;    /* should be initialized by caller */
  99.     l->next = (LINE *) NULL;
  100.  
  101.     return l;
  102. }
  103.  
  104. /*
  105.  * filealloc() - construct an initial empty file buffer 
  106.  */
  107. void
  108. filealloc()
  109. {
  110.     if ((Filemem->linep = newline(1)) == NULL) {
  111.     fprintf(stderr, "Unable to allocate file memory!\n");
  112.     getout(1);
  113.     }
  114.     if ((Filetop->linep = newline(1)) == NULL) {
  115.     fprintf(stderr, "Unable to allocate file memory!\n");
  116.     getout(1);
  117.     }
  118.     if ((Fileend->linep = newline(1)) == NULL) {
  119.     fprintf(stderr, "Unable to allocate file memory!\n");
  120.     getout(1);
  121.     }
  122.     Filemem->index = 0;
  123.     Filetop->index = 0;
  124.     Fileend->index = 0;
  125.  
  126.     Filetop->linep->prev = NULL;
  127.     Filetop->linep->next = Filemem->linep;    /* connect Filetop to Filemem */
  128.     Filemem->linep->prev = Filetop->linep;
  129.  
  130.     Filemem->linep->next = Fileend->linep;    /* connect Filemem to Fileend */
  131.     Fileend->linep->prev = Filemem->linep;
  132.     Fileend->linep->next = NULL;
  133.  
  134.     *Curschar = *Filemem;
  135.     *Topchar = *Filemem;
  136.  
  137.     Filemem->linep->num = 0;
  138.     Fileend->linep->num = 0xffffffffL;
  139.  
  140.     clrall();            /* clear all marks */
  141. }
  142.  
  143. /*
  144.  * freeall() - free the current buffer 
  145.  *
  146.  * Free all lines in the current buffer. 
  147.  */
  148. void
  149. freeall()
  150. {
  151.     LINE           *lp;
  152.     LINE           *xlp;
  153.     int             i;
  154.  
  155.     for (lp = Filetop->linep; lp != NULL; lp = xlp) {
  156.     if (lp->s != NULL)
  157.         free(lp->s);
  158.     xlp = lp->next;
  159.     free((char *) lp);
  160.     }
  161.  
  162.     Curschar->linep = NULL;    /* clear pointers */
  163.     Filemem->linep = NULL;
  164.     Filetop->linep = NULL;
  165.     Fileend->linep = NULL;
  166.  
  167.     for (i = 0; i < Rows; i++) {/* clear screen information */
  168.     LinePointers[i] = NULL;
  169.     LineSizes[i] = 0;
  170.     }
  171. }
  172.  
  173. /*
  174.  * canincrease(n) - returns TRUE if the current line can be increased 'n'
  175.  * bytes 
  176.  *
  177.  * This routine returns immediately if the requested space is available. If not,
  178.  * it attempts to allocate the space and adjust the data structures
  179.  * accordingly. If everything fails it returns FALSE. 
  180.  */
  181. bool_t
  182. canincrease(n)
  183.     register int    n;
  184. {
  185.     register int    nsize;
  186.     register char  *s;        /* pointer to new space */
  187.  
  188.     nsize = strlen(Curschar->linep->s) + 1 + n;    /* size required */
  189.  
  190.     if (nsize <= Curschar->linep->size)
  191.     return TRUE;
  192.  
  193.     /*
  194.      * Need to allocate more space for the string. Allow some extra space on
  195.      * the assumption that we may need it soon. This avoids excessive numbers
  196.      * of calls to malloc while entering new text. 
  197.      */
  198.     s = alloc((unsigned) (nsize + SLOP));
  199.     if (s == NULL) {
  200.     emsg("Can't add anything, file is too big!");
  201.     State = NORMAL;
  202.     return FALSE;
  203.     }
  204.     Curschar->linep->size = nsize + SLOP;
  205.     strcpy(s, Curschar->linep->s);
  206.     free(Curschar->linep->s);
  207.     Curschar->linep->s = s;
  208.  
  209.     return TRUE;
  210. }
  211.